package com.zoe.mallserver.receiver;

import com.rabbitmq.client.Channel;
import com.zoe.vhr.enums.MailConfigEnum;
import com.zoe.vhr.model.Employee;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author zoe.Zhang
 * @date 2023/01/05
 * @description
 */
@Component
public class DelayedListener {
    @RabbitListener(queues = MailConfigEnum.DELAYED_QUE)
    public void handler(Message<Employee> message, Channel channel) throws IOException {
        MessageHeaders headers = message.getHeaders();
        Long msgTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        String msgId = (String) headers.get(MailConfigEnum.CONSUMER_MSG_ID);
        try {
            Employee employee = message.getPayload();
            String join = String.join("\n", msgTag.toString(), msgId, employee.toString());
            System.out.println(join);
            System.out.println("死信处理消息成功");
            channel.basicAck(msgTag, false);
        }
        catch (Exception e) {
            System.out.println("死信丢弃消息");
            channel.basicNack(msgTag, false, false);
        }
    }
}
